package com.cygia.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cygia.common.Result;
import com.cygia.entity.Purchase;
import com.cygia.mapper.PurchaseMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@SuppressWarnings("ALL")
@RestController
@RequestMapping("/purchase")
public class PurchaseController {
    @Resource
    PurchaseMapper purchasemapper;

    /**
     * 查询采购订单
     *
     * @param pageNum  第几页
     * @param pageSize 每页多少条
     * @param gys      搜索供应商
     * @param cgy      搜索采购员
     * @return page 查询到的page对象返回给前端
     */
    @GetMapping("/pagePurchase")
    public Result<?> findPagePurchase(@RequestParam(defaultValue = "1") Integer pageNum,
                                      @RequestParam(defaultValue = "10") Integer pageSize,
                                      @RequestParam(defaultValue = "") String gys,
                                      @RequestParam(defaultValue = "") String cgy,
                                      @RequestParam(defaultValue = "2021-12-16") String cgrq_start,
                                      @RequestParam(defaultValue = "2021-12-18") String cgrq_end) {
        // 创建LambdaQueryWrapper
        LambdaQueryWrapper<Purchase> wrapper = Wrappers.lambdaQuery();
//        QueryWrapper<Purchase> queryWrapper = QueryGenerator.initQueryWrapper(purchase, req.getParameterMap());
//        Page<Purchase> page = new Page<Purchase>(pageNo, pageSize);
//        IPage<Purchase> pageList = iPurchaseService.page(page, queryWrapper);
        //查询供应商
        wrapper.like(Purchase::getGys, gys);

        //查询采购员
        wrapper.like(Purchase::getCgy, cgy);

        //查询采购日期开始
        wrapper.ge(Purchase::getCgrq, cgrq_start);

        //查询采购日期结束
        wrapper.le(Purchase::getCgrq, cgrq_end);

        // 输出访问IP
        Map<String, String> map = System.getenv();
        String userName = map.get("USERNAME");// 获取用户名
        String computerName = map.get("COMPUTERNAME");// 获取计算机名
        String userDomain = map.get("USERDOMAIN");// 获取计算机域名
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        System.out.println(df.format(new Date()) + "--用户名：" + userName + "--主机名：" + computerName + "--域：" + userDomain);

        // 分页查询
        IPage page = purchasemapper.selectPage(new Page(pageNum, pageSize), wrapper.orderByDesc(Purchase::getCgrq, Purchase::getGys, Purchase::getCgy));
        return Result.success(page);
    }

}
